import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class StudentMenu extends JFrame {
    private JTextField nameField;
    private JTextField birthField;
    private JTextField genderField;
    private JTextField collegeField;
    private JTextField majorField;
    private Student student;

    //获取学生信息的方法，返回Person类
    public Student queryStudent(String sID) {
        return (Student)Main.controller.personalInformationQuery(sID);
    }

    //显示查询信息的菜单
    private void showQueryMenu() {
        // 显示学生信息并让用户修改
        JFrame modifyStudentFrame = new JFrame("查询/修改学生信息");
        modifyStudentFrame.setSize(400, 300);
        modifyStudentFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        modifyStudentFrame.setLocationRelativeTo(null);

        JPanel modifyStudentPanel = new JPanel();
        modifyStudentPanel.setLayout(new GridLayout(6, 2));

        JLabel nameLabel = new JLabel("姓名:");
        nameField = new JTextField(student.getName());
        modifyStudentPanel.add(nameLabel);
        modifyStudentPanel.add(nameField);

        JLabel birthLabel = new JLabel("出生日期:");
        birthField = new JTextField(student.getBirth());
        modifyStudentPanel.add(birthLabel);
        modifyStudentPanel.add(birthField);

        JLabel genderLabel = new JLabel("性别:");
        genderField = new JTextField(student.getGender());
        modifyStudentPanel.add(genderLabel);
        modifyStudentPanel.add(genderField);

        JLabel collegeLabel = new JLabel("学院:");
        collegeField = new JTextField(student.getCollege());
        modifyStudentPanel.add(collegeLabel);
        modifyStudentPanel.add(collegeField);

        JLabel majorLabel = new JLabel("专业:");
        majorField = new JTextField(student.getMajor());
        modifyStudentPanel.add(majorLabel);
        modifyStudentPanel.add(majorField);

        JButton confirmButton = new JButton("确认");
        confirmButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name = nameField.getText().trim();
                String birth = birthField.getText().trim();
                String gender = genderField.getText().trim();
                String college = collegeField.getText().trim();
                String major = majorField.getText().trim();

                // 检查是否正确输入
                if (name.isEmpty() ||  birth.isEmpty() || gender.isEmpty() || college.isEmpty() || major.isEmpty()) {
                    JOptionPane.showMessageDialog(modifyStudentFrame, "请输入所有信息！", "错误", JOptionPane.ERROR_MESSAGE);
                }
                else {
                    // 更新学生信息
                    student.setName(name);
                    student.setSID(Login.currentAccount);
                    student.setBirth(birth);
                    student.setGender(gender);
                    student.setCollege(college);
                    student.setMajor(major);

                    // 保存学生信息到存储中

                    JOptionPane.showMessageDialog(modifyStudentFrame, "学生信息修改成功！", "成功", JOptionPane.INFORMATION_MESSAGE);

                    // 关闭窗口且清空文本框
                    modifyStudentFrame.dispose();
                    nameField.setText("");
                    birthField.setText("");
                    genderField.setText("");
                    collegeField.setText("");
                    majorField.setText("");
                }
            }
        });
        modifyStudentPanel.add(confirmButton);
        modifyStudentFrame.add(modifyStudentPanel);
        modifyStudentFrame.setVisible(true);
    }

    public StudentMenu() {
        setTitle("学生菜单");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel selectionPanel = new JPanel();
        selectionPanel.setLayout(new GridLayout(2, 1));

        JButton queryButton = new JButton("查询/修改信息");
        queryButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (Main.controller.logInAccountCheck(Login.currentAccount, Login.currentPwd)) {
                    String sID = Login.currentAccount;  // 根据实际情况获取当前账号对应的学号
                    student = queryStudent(sID);  // 调用查询学生信息的方法
                    boolean found = false;
                    if (student != null) {
                        found = true;
                        showQueryMenu();
                    }
                    if (!found) {
                        // 如果未找到对应学号的学生，显示提示信息
                        JOptionPane.showMessageDialog(selectionPanel, "该学号的学生不存在：" + sID, "错误", JOptionPane.ERROR_MESSAGE);
                    }
                }
                else {
                    // 用户验证不通过，弹出提示窗口或者其他处理方式
                }
            }
        });
        selectionPanel.add(queryButton);
        add(selectionPanel);

        JButton exitButton = new JButton("退出");
        exitButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();  // 关闭窗口
            }
        });
        selectionPanel.add(exitButton);
    }
}